package com.jack.tang.juc.demos;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
精准唤醒
*/
public class communicate4 {
    public static void main(String[] args) {
        Data3 data = new Data3();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.plus();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.minor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.plus();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.minor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();
    }
}

class Data3 {
    private int number = 0;
    //创建Lock锁
    private Lock lock = new ReentrantLock();
    //获得condition
    Condition conditionA = lock.newCondition();
    Condition conditionB = lock.newCondition();
    Condition conditionC = lock.newCondition();
    Condition conditionD = lock.newCondition();

    //如果number=0,则number+1
    public void plus() throws InterruptedException {
        lock.lock();//加锁
        try {
            if (Thread.currentThread().getName()=="A") {
                while (number != 0)
                    conditionA.await();//A等待
                number++;
                System.out.println(Thread.currentThread().getName() + "=>" + number);
                conditionB.signal();//唤醒B线程
            }
            if (Thread.currentThread().getName()=="C") {
                while (number != 0)
                    conditionC.await();//C等待
                number++;
                System.out.println(Thread.currentThread().getName() + "=>" + number);
                conditionD.signal();//唤醒D线程
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//解锁
        }
    }

    //如果number!=0,则number-1
    public void minor() throws InterruptedException {
        lock.lock();
        try {
            if (Thread.currentThread().getName()=="B") {
                while (number == 0)
                    conditionB.await();//B等待
                number--;
                System.out.println(Thread.currentThread().getName() + "=>" + number);
                conditionC.signal();//唤醒C线程
            }
            if (Thread.currentThread().getName()=="D") {
                while (number == 0)
                    conditionD.await();//D等待
                number--;
                System.out.println(Thread.currentThread().getName() + "=>" + number);
                conditionA.signal();//唤醒A线程
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

